﻿using System;
using System.Collections.Generic;
using System.Linq;

using KingNET.Controls;
using KingNET.Enum;

namespace KingNET.Engine
{
    class Game
    {

        /// <summary>
        /// Uniq ID
        /// </summary>
        public Guid GameID
        {
            get;
            private set;
        }

        /// <summary>
        /// Oyun modu. Bilgisayar / Ağ
        /// </summary>
        public GameMode GameMode
        {
            get;
            set;
        }

        /// <summary>
        /// 
        /// </summary>
        public bool IsPlaying
        {
            get;
            private set;
        }

        /// <summary>
        /// Oyundaki kartlar
        /// </summary>
        public List<Card> Cards
        {
            get;
            private set;
        }

        /// <summary>
        /// Oyuncular
        /// </summary>
        public List<Player> Players
        {
            get;
            private set;
        }

        public Game()
        {
            //

            Cards = new List<Card>();
            Players = new List<Player>();
            GameID = Guid.NewGuid();

        }

        /// <summary>
        /// Yeni bir oyun yada kayıt edilmiş oyunu başlatır
        /// </summary>
        public void Start(GameType gameType)
        {
            switch (gameType)
            {
                case GameType.New:
                    {
                        // TODO : Kart karıştırma
                        break;
                    }
                case GameType.Saved:
                    {
                        break;
                    }
            }

            //

            IsPlaying = true;
        }

        public void Stop()
        {
            IsPlaying = false;
        }

        #region ScaleCards
        /// <summary>
        /// Kartın boyutunu ölçeğe göre değiştirir
        /// </summary>
        /// <param name="scaleWidth">Öçlek</param>
        public void ScaleCards(float scaleWidth)
        {
            foreach (Player player in this.Players)
                foreach (Card card in player.Cards)
                    card.ScaleCard(scaleWidth);
        }
        #endregion

        #region ShuffleCards
        /// <summary>
        /// Oyundaki kartları rastgele karıştırır
        /// </summary>
        public void ShuffleCards()
        {
            Random random = new Random();

            for (int i = 0; i < this.Cards.Count; i++)
            {
                int j = random.Next(52);

                Card tempCard = this.Cards[i];
                this.Cards[i] = this.Cards[j];
                this.Cards[j] = tempCard;
            }
        }
        #endregion

        #region GetCards
        /// <summary>
        /// Oyuncu için rastgele 13 adet kart seçer
        /// </summary>
        /// <param name="player"></param>
        public void GetCards(Player player)
        {

            int i = 0;
            while (i < Constants.CARD_COUNT)
            {
                Random random = new Random();
                int randomCardIndex = random.Next(0, this.Cards.Count);

                player.Cards.Add(this.Cards[randomCardIndex]);

                this.Cards.RemoveAt(randomCardIndex);

                i++;
            }
        }
        #endregion

        public void DealCards()
        {
            //

            Cards.Clear();

            foreach (Player player in this.Players)
                player.Cards.Clear();

            // Kartlar

            foreach (Card card in Helper.GetDeck())
                Cards.Add(card);

            //

            this.ShuffleCards();

            //

            foreach (Player player in this.Players)
            {
                GetCards(player);
                player.Cards.OrderBy(c => c.Suit);
            }

        }

    }
}
